﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.ComponentModel;
using System.Threading;
using WebApp1.Controllers;
using System.IO;
using System.Web.Caching;

namespace WebApp1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {


        public MvcApplication()
        {
            this.Error += new EventHandler(MvcApplication_Error);
        }

      


        void MvcApplication_Error(object sender, EventArgs e)
        {
            Application_Error();
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {

            bool IsProxyModeEnabled = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings.Get("IsProxyModeEnabled"));

            if (IsProxyModeEnabled == true)
            {
                routes.MapRoute(
                  "NotFound",
                  "{*url}",
                  new { controller = "Home", action = "CatchAll" } // Parameter defaults
                );
                return;
            }

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
              "WebSiteInfo", // Route name
              "WebSiteInfo/{action}/{id}", // URL with parameters
              new { controller = "WebSiteInfo", action = "Index", id = UrlParameter.Optional } // Parameter defaults
          );

            routes.MapRoute(
             "Search", // Route name
             "Search/{q}/{page}", // URL with parameters
             new { controller = "Home", action = "Search", q = UrlParameter.Optional, page = 1 } // Parameter defaults
         );

            routes.MapRoute(
            "Source", // Route name
            "Source/{id}/{page}", // URL with parameters
            new { controller = "Home", action = "Source", id = UrlParameter.Optional, page = 1 } // Parameter defaults
        );

            routes.MapRoute(
           "Language", // Route name
           "Language/{id}/{page}", // URL with parameters
           new { controller = "Home", action = "Language", id = UrlParameter.Optional, page = 1 } // Parameter defaults
       );

            routes.MapRoute(
       "Go", // Route name
       "Go/{id}", // URL with parameters
       new { controller = "Home", action = "Go", id = UrlParameter.Optional } // Parameter defaults
   );
            routes.MapRoute(
      "fb", // Route name
      "fb/{id}", // URL with parameters
      new { controller = "Home", action = "fb", id = UrlParameter.Optional } // Parameter defaults
  );

            routes.MapRoute(
            "Category", // Route name
            "Category/{id}/{page}", // URL with parameters
            new { controller = "Home", action = "Category", id = UrlParameter.Optional, page = 1 } // Parameter defaults
        );

            routes.MapRoute(
          "Topic", // Route name
          "Topic/{id}/{page}", // URL with parameters
          new { controller = "Home", action = "Topic", id = UrlParameter.Optional, page = 1 } // Parameter defaults
      );



            routes.MapRoute(
               "Default", // Route name
               "{controller}/{action}/{id}/{page}", // URL with parameters
               new { controller = "Home", action = "Default", id = UrlParameter.Optional, page = 1 } // Parameter defaults
           );



            //routes.MapRoute(
            //  "NotFound",
            //  "{*url}",
            //  new { controller = "Home", action = "CatchAll"} // Parameter defaults
            //);


        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error()
        {
            //todo:
            //for now this is not used.
            // in future if there is a need to handle the exceptions in application level, we just need to use this code.

            return;

            Exception exception = Server.GetLastError();
            this.Response.Clear();

            HttpException httpException = exception as HttpException;

            if (httpException != null)
            {
                string action;
                Response.StatusCode = 200;
                Response.SuppressContent = true;
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        // page not found
                        Response.Write("404 error");
                        break;
                    case 500:
                        // server error
                        Response.Write("500 error");

                        break;
                    default:
                        Response.Write("default error");

                        break;
                }
            }
        }
    }
}